package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    //添加套餐
    @Override
    @Transactional//开启事务
    public void addSetmeal(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //把信息添加到套餐表
        setmealMapper.addSetmeal(setmeal);
        //获取生成的套餐id
        Long id = setmeal.getId();
        //把套餐id设置到套餐菜品关系中
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            }
        );
        //把信息添加到套餐菜品关系表
        setmealDishMapper.addBatch(setmealDishes);
    }

    //分页查询套餐
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //使用mybatis相应的插件查询
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //查询套餐信息
        List<SetmealVO> setmealVOS = setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<SetmealVO> p =(Page<SetmealVO>)setmealVOS;
        //封装PageBean对象
        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
        return pageResult;
    }

    //删除套餐
    @Override
    @Transactional//开启事务
    public void deleteSetmeals(List<Long> ids) {
        //如果要删除的菜品中包含起售的套餐则不能删除
        List<Setmeal> setmeals = setmealMapper.getByIds(ids);
        setmeals.forEach(new Consumer<Setmeal>() {
            @Override
            public void accept(Setmeal setmeal) {
                if(setmeal.getStatus() == StatusConstant.ENABLE){
                    //起售的菜品不能删除
                    throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
                }
            }
        });
        //删除套餐菜品关系表中的数据
        setmealDishMapper.deleteBySetmealIds(ids);
        //删除套餐表中的数据
        setmealMapper.deleteByIds(ids);
    }

    //起售/禁售套餐
    @Override
    public void startOrStop(Long id, Integer status) {
        //如果是起售的话，先判断套餐中有无禁用的套餐
        if(status == StatusConstant.ENABLE){
            //查询改套餐对应的菜品
            List<Dish> dishes = dishMapper.getBySetmealId(id);
            dishes.forEach(new Consumer<Dish>() {
                @Override
                public void accept(Dish dish) {
                    if(dish.getStatus() == StatusConstant.DISABLE){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            });
        }
        //更新套餐状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    //根据id查询套餐的详细信息
    @Override
    public SetmealVO getSetmealByIdWithDish(Long id) {
        //查询套餐表和套餐关系表
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        return setmealVO;
    }

    //修改套餐
    @Override
    @Transactional //开启事务
    public void update(SetmealDTO setmealDTO) {
        //1.更新套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //2.删除套餐菜品关系表中的数据
        Long id = setmealDTO.getId();
        List<Long> ids =  new ArrayList<>();
        ids.add(id);
        setmealDishMapper.deleteBySetmealIds(ids);
        //3.重新插入相关数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(new Consumer<SetmealDish>() {
            @Override
            public void accept(SetmealDish setmealDish) {
                setmealDish.setSetmealId(id);
            }
        });
        setmealDishMapper.addBatch(setmealDishes);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
